<!-- 创建人才培养方案 -->
<template>
  <div class="assessment-edit-plan-container">
    <div class="my-form-container" style="margin:0;width:100%;height: 100%">
      <el-form :model="tablelist" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <div style="display:flex">
          <el-form-item class="my-form-item" prop="mc">
            <div class="my-form-item-label">
              名称
              <span class="mustW">*</span>
            </div>
            <el-input v-model="tablelist.mc" style="width:80%" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item class="my-form-item" prop="bb">
            <div class="my-form-item-label"> 版本
              <span class="mustW">*</span>
            </div>
            <el-input v-model="tablelist.bb" style="width:80%" placeholder="请输入版本"></el-input>
          </el-form-item>
        </div>
        <div style="display:flex">
          <el-form-item class="my-form-item" prop="syzy">
            <div class="my-form-item-label">
              适用专业
              <span style="color: #ff3819">*</span>
            </div>
            <el-select v-model="tablelist.syzy" placeholder="请选择" class="bk" @input="handleChangeZY" clearable>
              <el-option v-for="(item, index) in syzyList" :key="index" :label="item.name" :value="index">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="my-form-item" prop="pyfafl">
            <div class="my-form-item-label">
              培养方案分类
              <span style="color: #ff3819">*</span>
            </div>
            <el-select v-model="tablelist.pyfafl" placeholder="请选择" class="bk" @input="handleChangePlan" clearable>
              <el-option v-for="(item, index) in kcflList" :key="index" :label="item.flmc" :value="index">
              </el-option>
            </el-select>
            <div style="margin: 22px 40px 0 0;position:relative">
              <!-- <el-button type="warning" icon="el-icon-setting" @click="pjByIdGuide">培养方案分类设置</el-button> -->
              <button @click="pjByIdGuide" style="
                  width: 150px;
                  height: 30px;
                  border: 1px solid #f86900;
                  border-radius: 6px;
                  color: #f86900;
                  background: #fef1e8;
                ">
                <img src="../../../assets/assessment/设置.png" style="
                    width: 17px;
                    height: 17px;
                    background-size: 100% 100%;
                    background-repeat: no-repeat;
                    position: absolute;
                    top: 12px;
                    left: 10px;
                  " />
                <span style="margin-left:20px">培养方案分类设置</span>
              </button>
            </div>
          </el-form-item>
        </div>
        <div style="display:flex">
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">额定总学分</div>
            <el-input v-model="tablelist.edzxf" placeholder="请输入额定总学分" type="number" style="width: 32.5%"></el-input>
          </el-form-item>
        </div>
        <div style="display:flex">
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">必修课额定学分</div>
            <el-input v-model="tablelist.bxkedxf" placeholder="请输入必修课额定学分" type="number" style="width:80%"></el-input>
          </el-form-item>
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">选修课额定学分</div>
            <el-input v-model="tablelist.xxkedxf" placeholder="请输入选修课额定学分" type="number" style="width:80%"></el-input>
          </el-form-item>
        </div>
        <div style="display:flex">
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">自修课额定学分</div>
            <el-input v-model="tablelist.zxkedxf" placeholder="请输入自修课额定学分" type="number" style="width:80%"></el-input>
          </el-form-item>
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">讲座课额定学分</div>
            <el-input v-model="tablelist.jzkedxf" placeholder="请输入讲座课额定学分" type="number" style="width:80%"></el-input>
          </el-form-item>
        </div>
        <div style="display:flex">
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">实践教学额定学分</div>
            <el-input v-model="tablelist.sjjxedxf" placeholder="请输入实践教学额定学分" type="number" style="width:80%"></el-input>
          </el-form-item>
          <el-form-item class="my-form-item">
            <div class="my-form-item-label">教育训练额定学分</div>
            <el-input v-model="tablelist.jyxledxf" placeholder="请输入教育训练额定学分" type="number" style="width:80%"></el-input>
          </el-form-item>
        </div>
        <div style="display:flex">
          <el-form-item class="my-form-item">
            <div class="my-form-item-label" prop="fj">附件</div>
            <el-upload class="upload-demo" ref="upload" action="https://jsonplaceholder.typicode.com/posts/"
              :file-list="fileList" :on-change="getFile" :auto-upload="true" :multiple="false" :on-remove="onRemove" :on-exceed="handleExceed" :http-request="handleUpload"
              style="margin: 22px 40px;width: 500px;margin: 22px 40px" :limit="1">
              <!-- <el-button size="small" type="primary" @click="lookFile" style="float:left;margin:4px 10px 0 0" :disabled="tablelist.fj ? false : true">浏览</el-button>
              <el-button slot="trigger" size="small" type="primary">选择文件</el-button><span style="color:red">（最大允许上传个数为1）</span>
            </el-upload>
              <button @click="lookFile" :disabled="tablelist.fj ? false : true" style="
                  width: 70px;
                  height: 30px;
                  border:1px solid #5230e9;
                  border-radius: 6px;
                  color: #5230e9;
                  background: #efebff;
                  float: left;
                  margin: 5px 10px 0 0;
                ">浏 览</button> -->
              <el-button size="small" type="primary" @click="lookFile" style="float:left;margin:4px 10px 0 0;width: 70px;
                  height: 30px;
                  border:1px solid #5230e9;
                  border-radius: 6px;
                  color: #5230e9;
                  background: #efebff;
                  float: left;
                  margin: 5px 10px 0 0;font-size: 14px;line-height: 10px;" :disabled="tablelist.fj ? false : true">浏览
              </el-button>
              <el-button slot="trigger" style="
                  width: 90px;
                  height: 30px;
                  border:1px solid #5230e9;
                  border-radius: 6px;
                  color: #5230e9;
                  background: #efebff;font-size: 14px;margin-top: 5px;line-height: 10px;" size="small" type="primary">
                选择文件</el-button>
              <!-- <span style="color:red">（最大允许上传个数为1）</span> -->
              <!-- <button slot="trigger" style="
                  width: 90px;
                  height: 30px;
                  border:1px solid #5230e9;
                  border-radius: 6px;
                  color: #5230e9;
                  background: #efebff;
                ">选择文件</button><span style="color:red">（最大允许上传个数为1）</span> -->
            </el-upload>
          </el-form-item>
        </div>
        <div style="display:flex;position:relative">
          <el-form-item class="my-form-item" style="height: 100px">
            <div class="my-form-item-label">说明</div>
            <el-input v-model="tablelist.sm" placeholder="请输入说明" class="zyjs" type="textarea"></el-input>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="button-group">
      <div @click="addSubmit">
        <div>保 存</div>
      </div>
      <div @click="cancalS">
        <div>取 消</div>
      </div>
      <!-- <el-button type="success" @click="addSubmit">保 存</el-button>
      <el-button type="danger" @click="cancalS" style="margin-left:60px">取 消</el-button> -->
    </div>
  </div>
</template>

<script>
import {
  reqUpdateTraining,
  getUploadTrain,
  queryFirstcoursetype
} from "@/api/propositionReview/approval";
import { szzszy } from "@/api/professional/details";
import { lineWord } from "@/api/admin1/check";

export default {
  data() {
    return {
      isHaveFile: false,
      zyId: '',
      fnId: '',
      syzy: "",
      kcfl: "",
      showYprDialog: false,
      fileList: [],
      fileName: "",
      tablelist: {
        mc: "",
        bb: "",
        syzy: "",
        pyfafl: "",
        planFlId: "",
        edzxf: "",
        bxkedxf: "",
        xxkedxf: "",
        zxkedxf: "",
        jzkedxf: "",
        sjjxedxf: "",
        jyxledxf: "",
        fj: "",
        sm: "",

      },
      rules: {
        mc: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        bb: [
          { required: true, message: "请输入版本", trigger: "blur" },
        ],
        syzy: [
          { required: true, message: '请选择适用专业', trigger: 'change' }
        ],
        pyfafl: [
          { required: true, message: "请选择培养方案分类", trigger: "change" },
        ]
      },
      syzyList: [],
      kcflList: [],
      successFile:''
    };
  },
  mounted() {
    this.getSpecialtyInfo();
    this.getInitInfo();
    this.tablelist = JSON.parse(localStorage.getItem('setDetails'));
    this.fileLoad(0)
    console.log(this.tablelist)
  },
  methods: {
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    //上传的附件浏览
    lookFile() {
      lineWord(this.tablelist.id,this.successFile).then(res=>{
        const blob = new Blob([res.data]);
        let url = URL.createObjectURL(blob);
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        let arr = this.successFile.split('.');
        if(arr[arr.length - 1] == 'doc' || arr[arr.length-1] == 'docx'){
          let href = window.location.origin + `/api/xpaas-plan-service/testtrainingprograminfo/lineWord?filePaths=${this.successFile}&id =${this.tablelist.id}`;
          window.open(href, '_blank');
        } else {   
          link.setAttribute("download",this.successFile.split(';')[1] + '.'  + arr[arr.length - 1]);
          document.body.appendChild(link);
          link.click();
        }
      })
    },
    //培养分类方案下拉框
    async getInitInfo() {
      const result = await queryFirstcoursetype();
      if (result.data.code == 200 && result.data.data.length > 0) {
        this.kcflList = result.data.data;
      } else {
        this.$message({ type: "error", message: "培养方案分类获取失败！" });
      }
    },
    //适用专业下拉框
    async getSpecialtyInfo() {
      const result = await szzszy();
      if (result.data.code == 200 && result.data.data.length > 0) {
        this.syzyList = result.data.data;
      } else {
        this.$message({ type: "error", message: "适用专业获取失败！" });
      }
    },
    //选择适用专业事件
    handleChangeZY(index) {
      this.tablelist.syzyid = this.syzyList[index].id;
      this.tablelist.syzy = this.syzyList[index].name;
    },
    //选择培养方案事件
    handleChangePlan(index) {
      this.tablelist.planFlId = this.kcflList[index].id;
      this.tablelist.pyfafl = this.kcflList[index].flmc;
    },
    // 培养方案分类设置
    pjByIdGuide() {
      this.$router.push({
        path: "/admin/training/classifications", //要跳转的页面的路由
      });
    },
    onRemove() {
      this.tablelist.fj = ''
      this.fileName = ''
      this.fileList = []
    },
    fileLoad(type) {
      if (type) {
        if (this.tablelist.fj) {
          this.fileList = [{ name: this.tablelist.fj, url: this.fileName }]
        } else {
          this.onRemove()
        }
      } else {
        if (this.tablelist.fj) {
          this.fileList = [{ name: this.tablelist.fj.split(';')[1], url: this.tablelist.fj }]
        } else {
          this.onRemove()
          // $('.el-upload-list').css('display', 'none');
        }
      }
    },
    getFile() {
      let files = this.$refs.upload.uploadFiles[0].raw
      this.fileName = files;
      this.tablelist.fj = files.name
      this.fileLoad(1)
    },
    //选择完文件直接上传
    handleUpload: function (file) {
      const that = this
      let formData = new FormData();
      formData.append('file', file.file);
      getUploadTrain(this.tablelist.id,formData).then(res => {
        if (res.data.data && res.data.code == 200) {
          this.successFile = res.data.data;
          that.$message({
            message: '文件上传成功！',
            type: 'success'
          })
        } else {
          that.$message({
            message: '文件上传失败！',
            type: 'warning'
          })
        }
      })
    },
    // 保存
    async addSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          let fd = new FormData();
          if (this.fileName) {
            fd.append("file", this.fileName);
            getUploadTrain(this.tablelist.id,fd).then((res) => {
              if (res.data.code == 200) {
                this.tablelist.fj = res.data.data;
                reqUpdateTraining(this.tablelist).then(r => {
                  if (r.data.code == 200) {
                    this.$message({ type: "success", message: "保存成功！" });
                    this.$refs.ruleForm.resetFields();
                    this.$router.push({
                      path: "/admin/training/trainings", //要跳转的页面的路由
                    });
                  }
                })
              }
            });
          } else {
            this.tablelist.fj == '';
            reqUpdateTraining(this.tablelist).then(res => {
              if (res.data.code == 200) {
                this.$message({ type: "success", message: "保存成功！" });
                this.$refs.ruleForm.resetFields();
                this.$router.push({
                  path: "/admin/training/trainings", //要跳转的页面的路由
                });
              }
            })
          }
        } else {
          console.log("操作失误!!");
        }
      })
    },
    // 取消
    cancalS() {
      this.$router.push({
        path: "/admin/training/trainings", //要跳转的页面的路由
      });
      // this.$refs[ruleForm].resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.el-upload-list {
  position: absolute;
  left: 26%;
  top: 25%;
}

/deep/.el-upload-list__item .el-icon-close-tip {
  position: absolute;
  top: 30px;
  right: 52px;
}

/deep/ .el-form-item__error {
  color: #F56C6C;
  font-size: 12px;
  padding-top: 4px;
  position: absolute;
  top: 75%;
  left: 32%;
}

.mustW {
  color: red;
}

.bk {
  /deep/ .el-input__inner {
    border-radius: 4px;
  }
}

.zyjs {
  width: 82.5% !important;
  margin: 20px auto;

  /deep/ .el-input__inner {
    height: 70px !important;
    border-radius: 4px;
    border: 1px solid #004ca7;
  }
}

.el-input,
.el-select {
  width: 26%;
  display: flex;
  justify-items: center;
  align-items: center;
  margin-left: 40px;
  margin-right: 40px;
}

.el-select:nth-child(-n+2) {
  width: 80%;
}

.my-form-item {
  width: 100%;
  height: 85px;
  border-bottom: 1px solid rgba(0, 76, 167, 0.39);
  margin-bottom: 0;

  >div {
    display: flex;
    height: 100%;

    &:nth-child(2) {
      .my-form-item-label {
        border-left: 1px solid rgba(0, 76, 167, 0.39);
      }
    }

    .my-form-item-label {
      width: 220px;
      padding-left: 60px;
      background: rgba(0, 76, 167, 0.1);
      display: flex;
      flex: none;
      align-items: center;
      color: #004CA7;
      font-size: 16px;
      font-weight: bold;
      border-left: 1px solid rgba(0, 76, 167, 0.39);
      margin-left: -1px;

      .mustW {
        color: red;
      }
    }

    .el-form-item__content {
      display: flex;
    }
  }
}

.el-date-editor {
  width: 38%;
}

.choose-ypr-container {
  width: 100%;
  margin: 0px auto;

  .choose-ypr-search-container {
    .el-input--prefix .el-input__inner {
      padding-left: 30px;
    }

    .el-input__inner {
      &::placeholder {
        color: #b8b8b8;
      }

      &::-webkit-input-placeholder {
        /* WebKit browsers 适配谷歌 */
        color: #b8b8b8;
      }

      &:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 适配火狐 */
        color: #b8b8b8;
      }

      &::-moz-placeholder {
        /* Mozilla Firefox 19+ 适配火狐 */
        color: #b8b8b8;
      }

      &:-ms-input-placeholder {
        /* Internet Explorer 10+  适配ie*/
        color: #b8b8b8;
      }
    }

    display: flex;
    flex-direction: row;
    align-items: center;

    .title {
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      line-height: 20px;
      color: #004ca7;
      margin-right: 5px;
    }

    .search-button {
      width: 77px;
      height: 28px;
      line-height: 28px;
      background: #f1f8fe;
      border: 1px solid #51aef6;
      border-radius: 6px;
      cursor: pointer;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;

      >span {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #51aef6;
      }

      >img {
        width: 20px;
        height: 20px;
        margin-right: 4px;
      }
    }

    .button-list {
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-left: 21px;

      .button-item {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        position: relative;
        width: 69px;
        height: 27px;
        line-height: 27px;
        border: 1px solid #004ca7;
        border-radius: 4px;
        margin-right: 10px;
        text-align: center;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #004ca7;

        .del-icon {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 12px;
          height: 12px;
          padding: 2px;
        }
      }
    }
  }

  .choose-ypr-container .choose-ypr-table-container .el-table__header th {
    padding: 0;
    height: 57px;
    background-color: #edf2f9;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    line-height: 20px;
    color: #004ca7;
  }

  /deep/ .el-table .cell {
    color: #004ca7;
  }

  .choose-ypr-table-container {
    margin-top: 30px;

    .el-table td,
    .el-table th.is-leaf,
    .el-table--border,
    .el-table--group {
      border-color: #c7d7eb;
    }

    .el-table__header tr,
    .el-table__header th {
      padding: 0;
      height: 57px;
      background-color: #edf2f9;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      line-height: 20px;
      color: #004ca7;
    }

    .el-table__body tr,
    .el-table__body td {
      padding: 0;
      height: 51px;
    }

    .table-data {
      width: 100%;
      border: 0.8px solid #004ca7;
      border-radius: 9px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #004ca7;
    }
  }
}

.assessment-container {
  .el-dialog__body {
    height: 251px;
  }

  .el-dialog__footer {
    padding: 10px 20px 45px;
  }

  .el-checkbox__inner:hover {
    border-color: #004ca7;
  }

  .el-checkbox__inner {
    border: 1px solid #004ca7;
    border-radius: 0;
    height: 18px;
    width: 18px;
  }

  .el-checkbox__inner::after {
    height: 10px;
    left: 6px;
  }

  .el-checkbox__input.is-checked .el-checkbox__inner,
  .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background-color: #004ca7;
    border-color: #004ca7;
  }

  .el-checkbox__input.is-checked+.el-checkbox__label {
    color: #004ca7;
  }

  .el-checkbox.is-bordered.is-checked {
    border-color: #004ca7;
  }

  .el-checkbox__input.is-focus .el-checkbox__inner {
    border-color: #004ca7;
  }
}

.cz-save {
  box-sizing: border-box;
  line-height: 44px;
  border-radius: 4px;
  font-size: 12px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  text-align: center;
  margin-top: 70px;
  margin-left: 40px;
  display: flex;
  flex-direction: row;
  cursor: pointer;
  align-content: center;
  justify-content: center;
  align-items: center;
}

.jf-save {
  display: flex;
  align-items: center;
  margin-left: 48px;
}

.fileClass {
  position: relative;
  cursor: pointer;

  .save {
    width: 120px;
    height: 50px;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    color: #fff;
    background: #303133;
  }
}

.my-form-item-value {
  font-size: 16px;
  color: #004ca7;
  height: 100%;
  flex: 1;
  line-height: 60px;
  padding-left: 38px;
  display: flex;
  align-items: center;
}

.assessment-edit-plan-container {
  .assessment-design {
    .el-textarea__inner {
      resize: none;
      font-size: 16px;
      color: rgba(0, 76, 167, 1);
    }
  }
}
</style>
<style scoped lang="less">
.assessment-edit-plan-container {
  .assessment-design {
    padding: 25px 38px !important;
  }

  .button-group {
    cursor: pointer;
    margin-top: 50px;
    text-align: center;

    >div {
      display: inline-block;
      font-size: 18px;
      padding: 6px 22px;
      border-radius: 6px;

      &:nth-child(1) {
        color: #f3b815;
        border: 1px solid #f3b815;
        background: #fff6ef;
        margin-right: 140px;
      }

      &:nth-child(2) {
        color: #ff3819;
        border: 1px solid #ff3819;
        background: #ffefef;
      }
    }
  }
}
</style>
